package cn.tmall.controller;

import cn.tmall.pojo.Product;
import cn.tmall.pojo.PropertyValue;
import cn.tmall.service.ProductService;
import cn.tmall.service.PropertyService;
import cn.tmall.service.PropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("")
public class PropertyValueController {
    @Resource(name = "PropertyValueServiceImpl")
    PropertyValueService propertyValueService;
    @Resource(name = "ProductServiceImpl")
    ProductService productService;

    @RequestMapping("admin_propertyValue_edit")
    public String edit(Model model, int pid) {
        Product product = productService.get(pid);
        propertyValueService.init(product); //若该产品的属性值没有事先设定，则初始化为空
        List<PropertyValue> propertyValueList = propertyValueService.list(pid);

        model.addAttribute("propertyValueList", propertyValueList);
        model.addAttribute("product", product);
        return "admin/editPropertyValue";
    }

    @RequestMapping("admin_propertyValue_update")
    public String update(PropertyValue propertyValue) {
        propertyValueService.update(propertyValue);
        return "success";
    }
}
